<template>
  <div class="autoDetail">
    <h1 class="back-title"><i @click="$router.go(-1)" class="iconfont icon-fanhui"></i>{{!id?'添加新的':'编辑'}}邮件模板</h1>
    <el-form ref="form" :model="form" label-width="90px" label-position="left" :rules="rules">
      <el-form-item label="模块名称:" prop="name">
        <el-input v-model="form.name" placeholder="请输入模块名称" maxlength="20" size="small" style="width:260px"></el-input>
      </el-form-item>
      <el-form-item label="是否共享:" prop="isShare">
        <el-radio v-model="form.isShare" :label="true">是</el-radio>
        <el-radio v-model="form.isShare" :label="false">否</el-radio>
      </el-form-item>
      <el-form-item label="邮件主题:" prop="title">
        <el-input v-model="form.title" placeholder="请输入邮件主题" size="small" style="width:260px"></el-input>
      </el-form-item>
      <el-form-item label="邮件内容:" prop="content" class="spec">
        <!-- 文本编辑器-->
        <MakeBtnAuto ref="makeBtnAuto" @getInfoMake="getInfoMake"></MakeBtnAuto>
      </el-form-item>
      <el-form-item class="common-btn">
        <el-button size="small" @click="$router.go(-1)">取 消</el-button>
        <el-button  size="small" type="primary" id="submitEmail" @click="onSubmit('form')">确 定</el-button>
      </el-form-item>
    </el-form>
    <VueTour ref="vueTour" :steps="steps" prev="/automation/template/email"
             type="0" path="/automation/mailAutomation"></VueTour>
  </div>
</template>

<script>
    import {saveMailTemplate, mailTemplateDetail, editMailTemplate} from '@/api/automation/mail'
    import MakeBtnAuto from '@/components/mail/write/MakeBtnAuto'
    import {mapState} from 'vuex'
    import VueTour from '@/components/automation/VueRour'

    export default {
        name: "addMail",
        components: {MakeBtnAuto, VueTour},
        data() {
            return {
                form: {
                    name: '',
                    isShare: false,
                    title: '',
                    content: '',
                    senderName: '',
                    senderEmail: '',
                    attaches: [],//附件列表
                },
                value: '',
                options: [],
                dialogTemp: true,
                rules: {
                    name: [
                        {required: true, message: '请输入模块名称', trigger: 'blur'},
                        {min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
                    ],
                    isShare: [
                        {required: true, message: '请选择是否共享', trigger: 'change'}
                    ],
                    title: [
                        {required: true, message: '请输入邮件主题', trigger: 'blur'}
                    ],
                    content: [
                        {required: true, message: '请输入邮件内容', trigger: 'blur'},
                    ],
                    senderEmail: [
                        {required: true, message: '请选择发件人', trigger: 'change'}
                    ]
                },
                id: '',
                steps: [
                    {
                        target: '#submitEmail',  // We're using document.querySelector() under the hood
                        content: '【邮件自动化规则创建】<br>第二步，点击保存按钮完成邮件模板创建'
                    },
                ]
            }
        },
        computed: {
            ...mapState(['common'])
        },
        created() {
            this.bus.$on('getTitle', () => {
                this.form.title = "Dear: #nickname#" + this.form.title
            })

        },
        mounted() {
            if (this.common.isFirstEmail) {
                this.$nextTick(() => {
                    this.$refs.vueTour.init();//初始化
                })
            }
            //获取默认邮箱
            this.common.emailList.map(item => {
                if (item.isDefault) {
                    this.form.senderEmail = item.emailAddr;
                    return;
                }
            })
            if(this.$route.path.indexOf('add')==-1){
                this.id= this.$route.params.id;
                this.getData();
            }
        },
        methods: {
            getData() {
                mailTemplateDetail(this.$route.params.id).then(res => {
                    if (res.data.success) {
                        this.form = res.data.data;
                        this.$refs.makeBtnAuto.init({
                            emailContent: this.form.content,
                            emailHtmlpicList: this.form.attaches ? this.form.attaches : []
                        });
                    }
                })
            },
            onSubmit(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if (!this.id) {//新增
                            saveMailTemplate(this.form).then(res => {
                                this.dealEnd(res);
                            })
                        } else {//编辑
                            editMailTemplate(this.id, this.form).then(res => {
                                this.dealEnd(res);
                            })
                        }
                    } else {
                        return false;
                    }
                });
            },
            dealEnd(res) {
                if (res.data.success) {
                    this.$message({
                        type: 'success',
                        message: res.data.msg
                    });
                    this.$router.go(-1)
                } else {
                    this.$message({
                        type: "success",
                        message: res.data.msg
                    });
                }

            },
            getInfoMake(params) {//文本编辑器数据 + 附件数据 emailContent emailHtmlpicList encodeURI
                this.form.attaches = params.emailHtmlpicList;
                this.form.content = encodeURIComponent(params.emailContent);
            },
        }
    }
</script>

<style lang="scss" scoped>
  .autoDetail {
    .edit {
      height: 400px;
      background-color: #F3F3F3;
    }
  }

</style>
